# from collections.abc import Iterable # 新版本
# # from collections import Iterable  # 老版本  会有警告DeprecationWarning:
#
# # Iterable 称之为可迭代对象
#
# # 数值类型
# print(isinstance(100, Iterable))
#
# # for i in 100:  # 'int' object is not iterable  int类型的对象不是一个可迭代的对象
# #     print(i)
#
# """怎么去检测是否为可迭代对象"""
# # 字符串类型
# print(isinstance('100', Iterable))
# # 列表
# print(isinstance([], Iterable))
# # 元组
# print(isinstance((1,), Iterable))
# # 字典
# print(isinstance({}, Iterable))
# # 集合
# print(isinstance(set(), Iterable))


from collections.abc import Iterator  # 迭代器
#
nums = [1, 2, 3] # 列表---->可迭代对象
nums = iter(nums)  # 获取迭代器
print(isinstance(nums, Iterator))  # 判断是否是迭代器
# # 取出迭代器的数据
# num1 = next(nums)
# print(num1)
# num1 = next(nums)
# print(num1)
# num1 = next(nums)
# print(num1)
# # StopIteration 报错   try except
# num1 = next(nums)
# print(num1)

# for循环内置了next()方法
# for i in nums:
#     print(i)

from collections.abc import Iterator,Iterable
class MyNumbers:
  def __iter__(self):  # 返回的是迭代器对象
    self.a = 1
    print(self,'self')
    return self  # 表示实例对象本身是自己的迭代器对象



  def __next__(self):
    self.a += 1  # 自增
    return self.a


myclass = MyNumbers() # 创建对象
print(isinstance(myclass,Iterable))
myiter = iter(myclass) # 获取迭代器
print(myiter,'myiter')
print(isinstance(myclass,Iterator))
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))